﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.Windows.Input;
using VisaCRM.Modules.Admin.ViewModels.Helper;
using VisaCRM.Modules.Admin.Model;
using VisaCRM.Modules.Admin.Gateway;
using VisaCRM.Common.Helper;
using Microsoft.Practices.Prism.Mvvm;

namespace VisaCRM.Modules.Admin.ViewModels
{
    /// <summary>
    /// ViewModel of an list of Where Hear
    /// </summary>
   public class WhereHearViewModel:BindableBase
    {
        /// <summary>
        /// initialises all collection used
        /// </summary>
       public ObservableCollection<WhereHear> allWhereHear;

       /// <summary>
       /// initialise gateway used
       /// </summary>
        WhereHearGateway whereHearGateway = new WhereHearGateway();

        private WebsiteDecideInfo websiteDecideInfo;

        public ICommand AddNewCommand { get; private set; }
        public ICommand SaveCommand { get; private set; }
        public ICommand CancelCommand { get; private set; }
        public ICommand DeleteCommand { get; private set; }
        public ICommand EditCommand { get; private set; }
        public ICommand CloseCommand { get; private set; }

        ///summary
        ///initialises a new instance of IndustryViewmodel class
        ///summary
        public WhereHearViewModel(ObservableCollection<WhereHear> allwherehere)
       {
           currentWhereHear = new WhereHear();
           
           if (allwherehere == null || allwherehere.Count == 0)
           {

               AllWhereHear = whereHearGateway.GetAllWhereHear();
           }
           else
           {
               this.AllWhereHear = allwherehere;
           }

           /// Re-act to any changes from outside this ViewModel
           this.AllWhereHear.CollectionChanged += (sender, e) =>
           {
               if (e.OldItems != null && e.OldItems.Contains(this.CurrentWhereHear))
               {
                   this.CurrentWhereHear = null;
               }
           };

          this.websiteDecideInfo = new WebsiteDecideInfo();
          this.WebsiteDecide = WebsiteDecide.Nothing;

           this.SaveCommand = new DelegateCommand((o) => this.Save());
           this.AddNewCommand = new DelegateCommand((o) => this.AddNew());
           this.CancelCommand = new DelegateCommand((o) => this.Cancel());
           this.DeleteCommand = new DelegateCommand((o) => this.Delete());
           this.EditCommand = new DelegateCommand((o) => this.Edit());
           this.CloseCommand = new DelegateCommand((o) => this.Close());
       }

        public WebsiteDecideInfo WebsiteDecideInfo
        {
            get { return this.websiteDecideInfo; }
            set
            {
                SetProperty(ref this.websiteDecideInfo, value);
                this.OnPropertyChanged(() => this.WebsiteDecide);
            }
        }

        public WebsiteDecide WebsiteDecide
        {
            get 
            { 
                return this.WebsiteDecideInfo.WebsiteDecide; 
            }
            set
            {
                this.WebsiteDecideInfo.WebsiteDecide = value;
                if (this.WebsiteDecide == WebsiteDecide.No)
                {
                    this.CurrentWhereHear.WhereHearWebsite = 0;
                }
                if (this.WebsiteDecide == WebsiteDecide.Yes)
                {
                    this.CurrentWhereHear.WhereHearWebsite = 1;
                }
                this.OnPropertyChanged("WebsiteDecide");
            }
        }

       private void Close()
       {
           throw new NotImplementedException();
       }

       private void Edit()
       {
           if (this.CurrentWhereHear != null)
           {
               WhereHear whereHear = new WhereHear() { WhereHearText = this.CurrentWhereHearName,WhereHearId=this.CurrentWhereHear.WhereHearId ,WhereHearWebsite=this.CurrentWhereHear.WhereHearWebsite };
               whereHearGateway.EditWhereHearToDatabase(this.CurrentWhereHearName,this.currentWhereHear.WhereHearWebsite, this.currentWhereHear.WhereHearId);
               this.AllWhereHear = whereHearGateway.GetAllWhereHear();
               this.CurrentWhereHear = whereHear;
               System.Windows.Forms.MessageBox.Show("The where hear details is editted sucessfully");
           }
           else
           {
               System.Windows.Forms.MessageBox.Show("Please select where hear details to be edited");
           }
       }

       private void Delete()
       {
           if (this.CurrentWhereHear != null)
           {
               whereHearGateway.DeleteWhereHearFromDatabase(this.currentWhereHear.WhereHearId);
               AllWhereHear.Remove(this.CurrentWhereHear);
               AddNew();
               System.Windows.Forms.MessageBox.Show("The where hear details is deleted sucessfully");
           }
           else
           {
               System.Windows.Forms.MessageBox.Show("Please selsect where hear details to be deleted");
           }
       }

       private void Cancel()
       {
           this.currentWhereHear = null;
       }

       private void AddNew()
       {
           this.CurrentWhereHearName = null;
           this.WebsiteDecide = WebsiteDecide.Nothing;
       }

       private void Save()
       {
           if (this.CurrentWhereHearName != null)
           {
               WhereHear whereHear = new WhereHear() { WhereHearText = this.CurrentWhereHearName };
               bool flag = false;
               if (string.IsNullOrEmpty(whereHear.WhereHearText))
                   return;
               if (string.IsNullOrEmpty(whereHear.WhereHearWebsite.ToString()))
                   return;
               foreach (WhereHear wh in this.AllWhereHear)
               {
                   if (wh.WhereHearText == whereHear.WhereHearText)
                   {
                       flag = true;
                       break;
                   }
               }
               if (flag)
               {
                   System.Windows.Forms.MessageBox.Show("The WhereHear details are already listed");
                   return;
               }
               whereHearGateway.SaveNewWhereHearToDatabase(this.CurrentWhereHearName,this.currentWhereHear.WhereHearWebsite);
               AddNew();
               this.AllWhereHear = whereHearGateway.GetAllWhereHear();
               System.Windows.Forms.MessageBox.Show("The WhereHere details are saved sucessfully");
           }
           else
           {
               System.Windows.Forms.MessageBox.Show("Please fill WhereHere and visible on website button details");
           }
       }

       /// <summary>
       /// The Where Hear currently selected in the workspace
       /// </summary>
       private WhereHear currentWhereHear;

       /// <summary>
       /// Gets or sets the WhereHear currently selected in the workspace
       /// </summary>
       public WhereHear CurrentWhereHear
       {
           get
           {
               return this.currentWhereHear;
           }

           set
           {
               this.currentWhereHear = value;
               if (this.CurrentWhereHear != null)
               {
                   this.CurrentWhereHearName = this.CurrentWhereHear.WhereHearText;
                   if (this.CurrentWhereHear.WhereHearWebsite == 0)
                   {
                       this.WebsiteDecide = WebsiteDecide.No;
                   }
                   if (this.CurrentWhereHear.WhereHearWebsite == 1)
                   {
                       this.WebsiteDecide = WebsiteDecide.Yes;
                   }
               }
               this.OnPropertyChanged("CurrentWhereHear");
           }
       }

       /// <summary>
       /// the WhereHearName currently selected in the workspace
       /// </summary>
       private string currentWhereHearName;
       /// <summary>
       /// Gets or sets the WhereHearName currently selected in the workspace
       /// </summary>
       public string CurrentWhereHearName
       {
           get
           {
               return this.currentWhereHearName;
           }

           set
           {
               this.currentWhereHearName = value;
               this.OnPropertyChanged("CurrentWhereHearName");
           }
       }

       /// <summary>
       /// Gets or sets the whereHear collection
       /// </summary>
       public ObservableCollection<WhereHear> AllWhereHear
       {
           get
           {
               return this.allWhereHear;
           }

           set
           {
               this.allWhereHear = value;
               this.OnPropertyChanged("AllWhereHear");
           }
       }
    }
}
